package net.phoenix.repository.property;

import java.io.InputStream;

import javax.jcr.Binary;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import javax.jcr.ValueFactory;
import javax.jcr.ValueFormatException;
import javax.jcr.nodetype.PropertyDefinition;

import net.phoenix.repository.BasicNode;
import net.phoenix.repository.RepositoryRuntimeException;
import net.phoenix.repository.dao.BlobBinary;
import net.phoenix.repository.value.BinaryValue;

/**
 * 
 * Jigsaw server
 * 
 * @author <a href="shamphone@gmail.com">Li XiongFeng</a>
 * @date 2012-1-3
 * @version 1.0.0
 */
public class BinaryProperty extends BasicProperty {
	/**
	 * create single value property
	 * 
	 * @param node
	 * @param definition
	 * @param value
	 */
	public BinaryProperty(BasicNode node, PropertyDefinition definition, Value value) {
		super(node, definition, value);
		try {
			if(value!=null)
			((BlobBinary) ((BinaryValue) value).getBinary()).attachNode(node);
		} catch (RepositoryException e) {
			throw new RepositoryRuntimeException(e);
		}

	}

	/**
	 * create multiple values property
	 * 
	 * @param node
	 * @param definition
	 * @param values
	 */
	public BinaryProperty(BasicNode node, PropertyDefinition definition, Value[] values) {
		super(node, definition, values);
		for (int i = 0; i < values.length; i++)
			try {
				((BlobBinary) ((BinaryValue) values[i]).getBinary()).attachNode(node);
			} catch (RepositoryException e) {
				throw new RepositoryRuntimeException(e);
			}
	}

	/**
	 * create a property with default value;
	 * 
	 * @param node
	 * @param definition
	 */
	public BinaryProperty(BasicNode node, PropertyDefinition definition) {
		super(node, definition);

	}

//	@Override
//	public void index(Document document) throws RepositoryException {
//		PropertyDefinition definition = this.getDefinition();
//		if (definition.isFullTextSearchable()) {
//			for (Value value : this.values) {
//				document.add(new Field(definition.getName(), value.getString(), Field.Store.YES, Field.Index.ANALYZED));
//			}
//		}
//	}

	@Override
	public String[] getArray() throws ValueFormatException {
		String[] array = new String[this.values.length];
		try {
			for (int i = 0; i < this.values.length; i++) {
				array[i] = ((BinaryValue) this.values[i]).getString();
			}
		} catch (ValueFormatException e) {
			throw e;
		} catch (RepositoryException ex) {
			throw new ValueFormatException(ex);
		}
		return array;
	}

	/**
	 * @return an <code>long</code>.
	 * 
	 * @return an <code>long</code>.
	 * @throws RepositoryException
	 */
	public long getLength() throws RepositoryException {
		long[] lengthes = this.getLengths();
		long length = 0;
		for (int i = 0; i < lengthes.length; i++)
			length += lengthes[i];
		return length;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.phoenix.repository.property.BasicProperty#getLengths()
	 */
	public long[] getLengths() throws RepositoryException {
		long[] lengths = new long[this.values.length];
		for (int i = 0; i < lengths.length; i++)
			lengths[i] = ((BinaryValue) this.values[i]).getBinary().getSize();
		return lengths;
	}

	/**
	 * 
	 * @param value
	 *            InputStream
	 * @throws RepositoryException
	 *             数据格式转换错误
	 */
	@Override
	public void setValue(Binary value) throws RepositoryException {
		this.modified = true;
		this.setValue(this.getValueFactory().createValue(value));
	}

	/**
	 * 
	 * @param value
	 *            InputStream
	 * @throws RepositoryException
	 *             数据格式转换错误
	 */
	@Override
	public void setValue(InputStream value) throws RepositoryException {
		this.modified = true;
		ValueFactory factory = this.getValueFactory();
		this.setValue(factory.createValue(factory.createBinary(value)));
	}

}
